package org.custom.spring.boot.mybatis.service.plugin.process.impl;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Invocation;
import org.custom.spring.boot.mybatis.service.plugin.process.MybatisProcessor;
import org.custom.spring.boot.mybatis.utils.MybatisExecutorUtil;

/**
 * mybatis乐观锁插件处理器
 *
 * @author 李坤
 * @date 2022/2/11 15:10
 */
public class OptimisticLockMybatisProcessor implements MybatisProcessor {
    @Override
    public Object apply(Invocation invocation, String finalSql) throws Throwable {
        Object[] args = invocation.getArgs();
        // 从参数中获取参数映射对象
        MappedStatement statement = (MappedStatement) args[0];
        // 将改造后的 SQL 绑定到元数据对象中
        MybatisExecutorUtil.resetParameter2Invocation(invocation, statement, statement.getBoundSql(args[1]), finalSql);
        return invocation.proceed();
    }
}
